<template>
  <div>
    <h4>商品清单如下:</h4>
    <div v-for="(item, index) in arr" :key="index">
      {{ item.shopName }} -- {{ item.price }}元/份
    </div>
    <h4>请选择购买数量</h4>
    <Food
      v-for="(item, index) in arr"
      :key="index"
      :name="item.shopName"
      :count="item.count"
      :index="index"
      @addE="addFn"
      @secE="secFn"
    ></Food>
    <p>总价为:{{ allPrice }}元</p>
  </div>
</template>

<script>
import Food from "./components/Food";
export default {
  data() {
    return {
      arr: [
        {
          shopName: "可比克薯片",
          price: 5.5,
          count: +0,
        },
        {
          shopName: "草莓酱",
          price: 3.5,
          count: +0,
        },
        {
          shopName: "红烧肉",
          price: 55,
          count: +0,
        },
        {
          shopName: "方便面",
          price: 12,
          count: +0,
        },
      ],
    };
  },
  components: {
    Food,
  },
  methods: {
    addFn(index) {
      this.arr[index].count++;
    },
    secFn(index) {
      this.arr[index].count > 0 && this.arr[index].count--;
    },
  },
  computed: {
    allPrice() {
      return this.arr.reduce((sum, obj) => (sum += obj.count * obj.price), 0);
    },
  },
};
</script>

<style>
</style>